Job會創建一個或多個pod,並在工作完成後終止(像是資料庫的合併、升級或是備份)。刪除Job將清除它創建的Pod。
job demo:
apiVersion: batch/v1
kind: Job
metadata:
name: job-demo
spec:
template:
spec:
containers:
- name: test
image: busybox
command: ["echo", "hi"]
restartPolicy: Never
部屬Job
root@master:/# kubectl apply -f job.yaml
job.batch/job-demo created
Get Job
root@master:/# kubectl get job
NAME COMPLETIONS DURATION AGE
job-demo 1/1 7s 7s
Get Pod
root@master:/# kubectl get pods
NAME READY STATUS RESTARTS AGE
job-demo-c7dxt 0/1 Completed 0 14s
Get Pod log
root@master:/# kubectl logs job-demo-c7dxt
hi
Linux中有crontab,而k8s的CronJob提供了類似的功能,可以定時執行Job,刪除CronJob將清除它創建的Pod。
Cronjob demo:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cronjob-demo
spec:
schedule: "*/1 * * * *" # 此為每1分鐘就執行一次
jobTemplate:
spec:
template:
spec:
containers:
- name: test
image: busybox
command: ["echo", "hi"]
restartPolicy: Never
部屬CronJob
root@master:/# kubectl apply -f cronjob.yaml
cronjob.batch/cronjob-demo created
Get CronJob
root@master:/# kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob-demo */1 * * * * False 0 22s 3m49s
Get Pod
root@master:/# kubectl get pods
NAME READY STATUS RESTARTS AGE
cronjob-demo-1661852520-z6nx5 0/1 Completed 0 2m25s
cronjob-demo-1661852580-jrrk8 0/1 Completed 0 85s
cronjob-demo-1661852640-mlptg 0/1 Completed 0 25s
Get Pod log
root@master:/# kubectl logs cronjob-demo-1661852520-z6nx5
hi